/*
 * NumberFormatTransformer.java
 *
 * Created on November 4, 2006, 10:05 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.circle;

import edu.umn.cs5115.scheduler.framework.gui.ValueTransformer;

import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.text.NumberFormatter;

/**
 * Formats numbers for display in a transformation.
 * @author grant
 */
public class NumberFormatTransformer implements ValueTransformer
{
    /** Number formatter to use to do the transformation */
    private NumberFormatter formatter;
    
    public NumberFormatTransformer(NumberFormat formatToUse)
    {
        formatter = new NumberFormatter(formatToUse);
    }
    
    public Object transform(Object value)
    {
        if (value instanceof Number)
        {
            try
            {
                return formatter.valueToString(value);
            } catch (ParseException ex) {
                return value;
            }
        } else
            return value;
    }

    public Object reverseTransform(Object value)
    {
        if (value instanceof String)
            
            try
            {
                return formatter.stringToValue((String)value);
            } catch (ParseException ex) {
                return value;
            }
        else
            return value;
    }
    
}
